//
//  NewsViewController.m
//  项目1 CLCMovie
//
//  Created by huiwenjiaoyu on 15/7/17.
//  Copyright (c) 2015年 CLC. All rights reserved.
//

#import "NewsViewController.h"
#import "UIViewExt.h"
#import "UIImageView+WebCache.h"
#import "MainUITabBarController.h"

@interface NewsViewController ()
{
    UITableView* _tab;
    NSMutableArray* _dataArray;
    UIImageView* _headImg;
    UILabel* _headLabel;
    
}
@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"新闻"];
    [self _loadData];
    [self _creatTableView];
    [self _creatHeadImg];
//    [self _hideTabBar];

    // Do any additional setup after loading the view.
}

//#pragma mark - TabBarHidden
//- (void)_hideTabBar
//{
//    self.navigationController.delegate = self;
//}
//
//- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
//{
//    NSLog(@"A will Show");
//    BOOL willHidden = NO;
//    if([viewController isEqual:self])
//        willHidden = NO;
//    else
//        willHidden = YES;
//    
//    [self TabBarHiddenOrShow:willHidden];
//}
//
//
//
//- (void)TabBarHiddenOrShow:(BOOL)willHidden
//{
//    [UIView beginAnimations:nil context:nil];
//    [UIView setAnimationDuration:10];
//    CGRect rect = self.tabBarController.tabBar.frame;
//    if(willHidden)
//        rect.origin.x = -kWidth;
//    else
//        rect.origin.x = 0;
//    [UIView commitAnimations];
//    self.tabBarController.tabBar.frame = rect;
//
//}

//- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
//{
//    NSLog(@"A did Show");
//}
//
#pragma mark 创建Tableview

- (void)_creatTableView
{
    
    //创建TableView
    _tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHieght-108) style:UITableViewStylePlain];
    _tab.delegate = self;
    _tab.dataSource = self;
    
    //注册复用池
    UINib* nib = [UINib nibWithNibName:@"NewsCellView" bundle:[NSBundle mainBundle]];
    [_tab registerNib:nib forCellReuseIdentifier:@"newsCell"];
    
    
    [self.view addSubview:_tab];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_dataArray count];
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 250;
//}
#pragma mark - 透明头视图/Cell高度设置


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 67;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 200;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 150)];
    view.backgroundColor = [UIColor clearColor];
    
    return view;
}


//{
//    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    btn.frame = CGRectMake(0, 0, kWidth, 67);
//    btn.tag =
//    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
//
//}
//
//- (void)btnAction:(UIButton*)btn
//{
//    NSLog(@"%@",btn.tag);
//}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsCell* cell = [tableView dequeueReusableCellWithIdentifier:@"newsCell" forIndexPath:indexPath];
 
    
    cell.newsModal = _dataArray[indexPath.row];
    [cell.Button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (void)btnAction:(UIButton*)btn
{
    NewsPictureViewController* Picture = [[NewsPictureViewController alloc] init];
    Picture.view.backgroundColor = [UIColor blackColor];

    Picture.hidesBottomBarWhenPushed = YES;
    [Picture.navigationItem.backBarButtonItem setTintColor:[UIColor whiteColor]];
    [self.navigationController pushViewController:Picture animated:YES];

}


#pragma mark - 可拉升顶部视图

- (void)_creatHeadImg
{

    _headImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 200)];
    [_headImg sd_setImageWithURL:[_dataArray[0] urlForImage]];
    


    _headLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _headImg.bottom-30, kWidth, 30)];
    _headLabel.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.5 alpha:0.5];
    _headLabel.textColor = [UIColor whiteColor];
    _headLabel.text = [_dataArray[0] title];
    
    [_dataArray removeObjectAtIndex:0];
    
    [self.view addSubview:_headImg];
    [self.view addSubview:_headLabel];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offY = scrollView.contentOffset.y;
    
    
    if(offY >= 0)
    {
        _headImg.top = -offY;
    }
    else
    {
        float newHight = 200 - offY;
        float newWidth = (newHight/200)*kWidth;
        _headImg.frame = CGRectMake(-(newWidth - kWidth)/2, 0, newWidth, newHight);
    }
    
    _headLabel.bottom = _headImg.bottom;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - 数据加载
- (void)_loadData
{
    NSArray* newsArray = [DataService getArrayOrDictionNaryfromJosnWithNamed:@"news_list"];
    
    _dataArray = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < [newsArray count]; i++) {
        NSString* title = [newsArray[i] valueForKey:@"title"];
        NSString* detail = [newsArray[i] valueForKey:@"summary"];
        NSURL* url = [NSURL URLWithString:[newsArray[i] valueForKey:@"image"]];
        BOOL type = [[newsArray[i] valueForKey:@"type"] boolValue];
        NewModal* modal = [[NewModal alloc] initWithTitle:title andDetail:detail andImageURL:url andType:type];
        [_dataArray addObject:modal];
    }
    
    

}
@end
